﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace editor
{
    /// <summary>
    /// PrimitiveListControl.xaml 的交互逻辑
    /// </summary>
    public partial class PrimitiveListControl : UserControl
    {
        private cfg.DList _data;
        private cfg.TType _valueType;
        public PrimitiveListControl(cfg.DList data)
        {
            InitializeComponent();
            this.DataContext = data;
            this._data = data;

            this._valueType = (data.DType as cfg.TList).ValueType;
        }

        private void Append(object sender, RoutedEventArgs e)
        {
            _data.Add(_valueType.NewDefault());
        }

        private int GetIndex(object sender) { return _data.Value.IndexOf((sender as MenuItem).DataContext as cfg.DData); }

        private void Copy(object sender, RoutedEventArgs e)
        {
            var item = (sender as MenuItem).DataContext as cfg.DData;
            int index = _data.Value.IndexOf(item);
            if (index >= 0)
            {
                var newItem = item.Clone();
                _data.Insert(index + 1, newItem);
            }
        }

        private void InsertBefore(object sender, RoutedEventArgs e)
        {
            var item = (sender as MenuItem).DataContext as cfg.DData;
            int index = _data.Value.IndexOf(item);
            if (index >= 0)
            {
                var newItem = _valueType.NewDefault(); ;
                _data.Insert(index, newItem);
            }
        }

        private void InsertAfter(object sender, RoutedEventArgs e)
        {
            var item = (sender as MenuItem).DataContext as cfg.DData;
            int index = _data.Value.IndexOf(item);
            if (index >= 0)
            {
                var newItem = _valueType.NewDefault(); ;
                _data.Insert(index + 1, newItem);
            }
        }

        private void Delete(object sender, RoutedEventArgs e)
        {
            var item = (sender as MenuItem).DataContext as cfg.DData;
            int index = _data.Value.IndexOf(item);
            if (index >= 0)
            {
                _data.RemoveAt(index);
            }
        }
    }
}
